package com.hua.yuan.live;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hua.yuan.BaseFragment;
import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.live.adapter.LivePagerAdapter;
import com.hua.yuan.live.fragment.AnchorHistoryFragment;
import com.hua.yuan.live.fragment.AnchorResumeFragment;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveAttentationLoader;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveRoomInfoLoader;
import com.hua.yuan.live.repertory.cloud_data.res.RoomInfoBean;
import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;

/**
 * 主播详情
 */
public class AnchorDetailActivity extends BaseActivity
        implements LoaderManager.LoaderCallbacks<RoomInfoBean>,
        View.OnClickListener,
        LiveAttentationLoader.onAttentationCallback,
        ViewPager.OnPageChangeListener

{

    public static final int TASK_ID = 1002;

    private final int resutltCode = 1001;

    private int mAttentation;

    private String mToken;

    private int anchorId;

    private AnchorHistoryFragment mAnchorHistoryFragment;

    private AnchorResumeFragment mAnchorResumeFragment;

    private LivePagerAdapter mAdapter;

    private List<BaseFragment> fragmentList = new ArrayList<>();

    private ViewPager mViewPager;

    private TextView tv_focus_live_detail;

    private TextView tv_nickname_live_detail;

    private TextView tv_level_live_detail;

    private ImageView iv_heardurl_live_detail;

    private TextView tv_anchor_viewnumber;


    private LinearLayout llHistory;

    private LinearLayout llResumeInfo;

    private TextView tvHistory;

    private TextView tvResumeInfo;

    private View vHistory;

    private View vResumeInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_detail);
        initView();
        initData();
    }

    private void initView(){
        tv_nickname_live_detail = findViewById(R.id.tv_nickname_live_detail);
        tv_level_live_detail = findViewById(R.id.tv_level_live_detail);
        iv_heardurl_live_detail = findViewById(R.id.iv_heardurl_live_detail);
        tv_anchor_viewnumber=findViewById(R.id.tv_anchor_viewnumber);


        llHistory=findViewById(R.id.ll_history_live_history);
        llResumeInfo=findViewById(R.id.ll_info_live_history);
        tvHistory=findViewById(R.id.tv_resume_live_history);
        tvResumeInfo=findViewById(R.id.tv_history_live_history);
        vHistory=findViewById(R.id.v_history_live_history);
        vResumeInfo=findViewById(R.id.v_resume_live_history);
        tv_focus_live_detail = findViewById(R.id.tv_focus_live_detail);
        tv_focus_live_detail.setOnClickListener(this);
        tv_focus_live_detail.setText(mAttentation == 0 ? "未关注" : "关注");
        mViewPager = findViewById(R.id.vp_fragment_live_detail);
        llHistory.setOnClickListener(this);
        llResumeInfo.setOnClickListener(this);
    }

    private void initData(){

        LiveRoomInfoLoader loader = (LiveRoomInfoLoader) getSupportLoaderManager().initLoader(TASK_ID, null, this);
        mToken = PreferenceUtils.getString(Constents.TOKEN);
        anchorId = getIntent().getIntExtra(LiveModule.INTENT_ANCHOR_ID, 0);
        mAnchorResumeFragment = new AnchorResumeFragment();
        mAnchorHistoryFragment = AnchorHistoryFragment.newInstance(mToken, anchorId);
        fragmentList.add(mAnchorResumeFragment);
        fragmentList.add(mAnchorHistoryFragment);
        mAdapter = new LivePagerAdapter(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(this);
        //请求数据
        loader.requestData(mToken.trim(), anchorId);
        loader.forceLoad();
    }

    @NonNull
    @Override
    public Loader<RoomInfoBean> onCreateLoader(int id, @Nullable Bundle args) {
        return new LiveRoomInfoLoader(this);
    }

    @SuppressLint("StringFormatMatches")
    @Override
    public void onLoadFinished(@NonNull Loader<RoomInfoBean> loader, RoomInfoBean data) {

        if (mAnchorResumeFragment != null && (mAnchorResumeFragment.getUserVisibleHint() == true)) {

            mAnchorResumeFragment.setData(data);
            tv_nickname_live_detail.setText(TextUtils.isEmpty(data.getNickName())==true ? "" : data.getNickName());
            String level = data.getLevel();
            mAttentation=data.getHasAttentioned();
            tv_focus_live_detail.setText((mAttentation == 0 ? "关注" : "已关注"));
            tv_level_live_detail.setText(String.format(getResources().getString(R.string.live_level), level));
            //如果当前直播状态为0
            if(data.getIsLive()==1){
                tv_anchor_viewnumber.setVisibility(View.VISIBLE);
                tv_anchor_viewnumber.setText(String.format("%d在线",data.getViewingNum()));
            }else{
                tv_anchor_viewnumber.setVisibility(View.INVISIBLE);
            }
            if (!TextUtils.isEmpty(data.getHeadUrl())) {
                Glide.with(this)
                        .load(data.getHeadUrl())
                        .placeholder(R.mipmap.bg_default_liveing)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(iv_heardurl_live_detail);
            }
        }

    }

    @Override
    public void onLoaderReset(@NonNull Loader<RoomInfoBean> loader) {


    }

    @Override
    public void onBackPressed() {
        setResult(resutltCode);
        finish();
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.tv_focus_live_detail) {
            int attation=mAttentation==0?1:0;
            LiveAttentationLoader.attentation(mToken, anchorId, attation, this);
        }

        if(v.getId()==R.id.ll_info_live_history){
            mViewPager.setCurrentItem(0,true);
        }

        if(v.getId()==R.id.ll_history_live_history){
            mViewPager.setCurrentItem(1,true);
        }
    }

    @Override
    public void attentation(Integer isSuccess) {

        if (isSuccess == 0) {
            Toast.makeText(getApplicationContext(), "关注成功", Toast.LENGTH_SHORT).show();
            mAttentation = (mAttentation == 0 ? 1 : 0);
            tv_focus_live_detail.setText((mAttentation == 0 ? "关注" : "已关注"));
        } else {
            Toast.makeText(getApplicationContext(), "关注失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        switch (position){

            case 0:
                tvHistory.setTextColor(getResources().getColor((R.color.color_text_focus_live_detail)));
                tvResumeInfo.setTextColor(getResources().getColor(R.color.color_text_unfocus_live_detail));
                vHistory.setVisibility(View.GONE);
                vResumeInfo.setVisibility(View.VISIBLE);
                break;
            case 1:
                tvHistory.setTextColor(getResources().getColor(R.color.color_text_unfocus_live_detail));
                tvResumeInfo.setTextColor(getResources().getColor((R.color.color_text_focus_live_detail)));
                vHistory.setVisibility(View.VISIBLE);
                vResumeInfo.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
